/**
 * @Author: lll
 * @Email:li.lele@vpclub.cn
 * @Date: create in 2020/7/10
 */
package cn.vpclub.admin.web.home;

import cn.vpclub.admin.entity.ArticleEntity;
import cn.vpclub.admin.mapper.ArticleMapper;
import cn.vpclub.admin.param.ArticleRequestParam;
import cn.vpclub.admin.param.GetArticlePageByTabContentParam;
import cn.vpclub.admin.param.GetArticlePageParam;
import cn.vpclub.admin.util.BaseResponse;
import cn.vpclub.admin.util.ResponseCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping(value = "article")
public class ArticleController {
    Logger log = LoggerFactory.getLogger(ArticleController.class);
    @Autowired
    ArticleMapper articleMapper;

    @RequestMapping(value = "pushArticle", method = RequestMethod.POST)
    public BaseResponse addArticle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, @RequestBody @Validated ArticleEntity articleEntity) {
        String id = (String) httpServletRequest.getAttribute("userId");
        log.info("获取当前角色id=={}", id);
        articleEntity.setCreatedBy(id);
        articleMapper.insert(articleEntity);
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, null);
    }

    @RequestMapping(value = "selectMyArticle", method = RequestMethod.POST)
    public BaseResponse selectMyArticle(HttpServletRequest httpServletRequest) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("createdBy", httpServletRequest.getAttribute("userId"));
        List list = articleMapper.selectList(queryWrapper);
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, list);
    }

    @RequestMapping(value = "deleteMyArticle", method = RequestMethod.POST)
    public BaseResponse deleteMyArticle(@RequestBody @Validated ArticleRequestParam articleRequestParam, HttpServletRequest httpServletRequest) {
        articleMapper.deleteById(articleRequestParam.getArticleId());
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, null);
    }

    @RequestMapping(value = "getArticlePage", method = RequestMethod.POST)
    public BaseResponse getArticlePage(@RequestBody GetArticlePageParam getArticlePageParam) {
        Page page = new Page(getArticlePageParam.getPageCurrent(), getArticlePageParam.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("left(article_content,60) as articleContent", "id as id", "tab_content as tabContent", "article_title as articleTitle");
        queryWrapper.like(!getArticlePageParam.getKeyWord().equals(""), "article_title", getArticlePageParam.getKeyWord());
        IPage iPage = articleMapper.selectPage(page, queryWrapper);
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, iPage);
    }

    @RequestMapping(value = "selectArticleByTabContent", method = RequestMethod.POST)
    public BaseResponse selectArticleByTabContent(@RequestBody GetArticlePageByTabContentParam getArticlePageByTabContentParam) {
        Page page = new Page(getArticlePageByTabContentParam.getPageCurrent(), getArticlePageByTabContentParam.getPageSize());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("tab_content", getArticlePageByTabContentParam.getTabName());
        IPage iPage = articleMapper.selectPage(page, queryWrapper);
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, iPage);
    }

    @RequestMapping(value = "selectArticleById", method = RequestMethod.POST)
    public BaseResponse selectArticleById(@Validated @RequestBody ArticleRequestParam articleRequestParam) {
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, articleMapper.selectById(articleRequestParam.getArticleId()));
    }

    @RequestMapping(value = "selectArticleUserInfo", method = RequestMethod.POST)
    public BaseResponse selectArticleUserInfo(@Validated @RequestBody ArticleRequestParam articleRequestParam) {
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, articleMapper.selectArticleUserInfo(articleRequestParam.getArticleId()));
    }
}
